package com.liuxvru.project.system.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.liuxvru.common.annotation.Log;
import com.liuxvru.common.core.controller.BaseController;
import com.liuxvru.common.core.domain.AjaxResult;
import com.liuxvru.common.enums.BusinessType;
import com.liuxvru.project.domain.Flaw;
import com.liuxvru.project.service.IFlawService;
import com.liuxvru.common.utils.poi.ExcelUtil;
import com.liuxvru.common.core.page.TableDataInfo;

/**
 * 缺陷Controller
 *
 * @author liuxvru
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/project/flaw")
public class FlawController extends BaseController {
    @Autowired
    private IFlawService flawService;

/**
 * 查询缺陷列表
 */
@PreAuthorize("@ss.hasPermi('project:flaw:list')")
@GetMapping("/list")
    public TableDataInfo list(Flaw flaw) {
        startPage();
        List<Flaw> list = flawService.selectFlawList(flaw);
        return getDataTable(list);
    }

    /**
     * 导出缺陷列表
     */
    @PreAuthorize("@ss.hasPermi('project:flaw:export')")
    @Log(title = "缺陷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Flaw flaw) {
        List<Flaw> list = flawService.selectFlawList(flaw);
        ExcelUtil<Flaw> util = new ExcelUtil<Flaw>(Flaw. class);
        util.exportExcel(response, list, "缺陷数据");
    }

    /**
     * 获取缺陷详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:flaw:query')")
    @GetMapping(value = "/{flawId}")
    public AjaxResult getInfo(@PathVariable("flawId") Long flawId) {
        return success(flawService.getById(flawId));
    }

    /**
     * 新增缺陷
     */
    @PreAuthorize("@ss.hasPermi('project:flaw:add')")
    @Log(title = "缺陷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Flaw flaw) {
        return toAjax(flawService.save(flaw));
    }

    /**
     * 修改缺陷
     */
    @PreAuthorize("@ss.hasPermi('project:flaw:edit')")
    @Log(title = "缺陷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Flaw flaw) {
        return toAjax(flawService.updateById(flaw));
    }

    /**
     * 删除缺陷
     */
    @PreAuthorize("@ss.hasPermi('project:flaw:remove')")
    @Log(title = "缺陷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{flawIds}")
    public AjaxResult remove(@PathVariable Long[] flawIds) {
        return toAjax(flawService.removeByIds(Arrays.asList(flawIds)));
    }
}
